<?php
/**
 * Survey for Magento
 *
 * @category   Belitsoft
 * @package	Belitsoft_Survey
 * @author	 Belitsoft <bits@belitsoft.com>
 */

/* @var $this Belitsoft_Survey_Block_Adminhtml_Question_Edit_Fields_Pickone
 * @var $this Belitsoft_Survey_Block_Adminhtml_Question_Edit_Fields_Pickmany
 */
?>
<script type="text/javascript">
var fieldsSelectTemplate = '<div id="<?php echo $this->getFieldId(); ?>_{{index}}"  class="option-box"> ' +
	'<table class="option-header" cellpadding="0" cellspacing="0">' +
		'<thead>' +
			'<tr>' +
				'<th class="opt-type"><?php echo Mage::helper('belitsoft_survey')->__('Option'); ?></th>' +
                '<th class="opt-req"><?php echo Mage::helper('belitsoft_survey')->__('Position'); ?></th>' +
				'<th>&nbsp;</th>' +
			'</tr>' +
		'</thead>' +
		'<tbody>' +
			'<tr>' +
				'<td><?php echo $this->getFieldOptionField(); ?></td>' +
				'<td><?php echo $this->getFieldSortOrderField(); ?></td>' +
				'<td>&nbsp;<?php echo $this->getDeleteButtonHtml(); ?></td>' +
			'</tr>' +
		'</tbody>' +
	'</table>' +
'</div>';
var optionIndex = 0;
BITSSurvey = {};
BITSSurvey.Fields = Class.create();
BITSSurvey.Fields.prototype = {
	idLabel : '<?php echo $this->getFieldId(); ?>',
	top : '<?php echo $this->getFieldTopId(); ?>',
	templateSyntax : /(^|.|\\r|\\n)({{(\w+)}})/,
	templateText : '',
	itemsCount : 0,

	initialize : function(template) {
		this.templateText = template;
	},

	add : function(data) {
		if(!data){
			data = {};
		}
//		this.top = $('<?php echo $this->getFieldTopId(); ?>');

		data.index = this.itemsCount++;
		this.template = new Template(this.templateText, this.templateSyntax);
		Element.insert(this.top, {'after':this.template.evaluate(data)});
		this.top = $(this.idLabel+'_'+data.index);
		
		for(field_name in data) {
			if(field_name != 'index' && field_name != 'id'
				&& $(this.idLabel+'_'+data.index+'_'+field_name) && data[field_name])
			{
				$(this.idLabel+'_'+data.index+'_'+field_name).value = data[field_name];
			}
		}
	},

	remove : function(event){
		var element = $(Event.findElement(event, 'div'));
		if(element){
			Element.select(element, '.delete').each(function(elem){elem.value='1'});
			Element.select(element, ['input', 'select']).each(function(elem){elem.hide(); elem.className = '';});
			Element.hide(element);
		}
	},

	checkSave : function() {
		var check_all_empty = true;
		$('<?php echo $this->getFieldId(); ?>').select('[id$="_<?php echo $this->getFieldTextString(); ?>"]').each(function(elem) {
			if((elem.className != '') && elem.value) {
				check_all_empty = false;
			}
		});

		if(check_all_empty) {
			alert("<?php echo Mage::helper('belitsoft_survey')->__("Please, provide a question options"); ?>");
			return false;
		}

		return true;
	}
}

BITSSurvey.Operations = Class.create();
BITSSurvey.Operations.prototype = {
	initialize : function() {},

	save : function() {
		return bitsSurveyFields.checkSave();
	}
}


try {
	bitsSurveyFields = new BITSSurvey.Fields(fieldsSelectTemplate);
	surveyOperations = new BITSSurvey.Operations();
} catch(e) {
	alert(e);
}
</script>
<div class="entry-edit custom-options bundle" id="survey_fields_container">
	<div class="entry-edit-head">
		<h4><?php echo Mage::helper('belitsoft_survey')->__('Options'); ?></h4>
		<div class="right"><?php echo $this->getAddButtonHtml(); ?></div>
	</div>

	<div id="<?php echo $this->getFieldId(); ?>" class="box">
		<div id="<?php echo $this->getFieldTopId(); ?>"></div>
	</div>
</div>

<?php if($fields = $this->getFields()): ?>
<script type="text/javascript">
	<?php foreach ($fields as $data): ?>
		bitsSurveyFields.add(<?php echo $this->getParentBlock()->_toJson($data); ?>);
	<?php endforeach; ?>
</script>
<?php endif; ?>
